package com.sakura.mall.ware.undoLog.service.impl;

import com.sakura.mall.common.response.Page;
import com.sakura.mall.ware.undoLog.domain.entity.UndoLogEntity;
import com.sakura.mall.ware.undoLog.domain.request.AddUndoLogRequest;
import com.sakura.mall.ware.undoLog.domain.request.UpdateUndoLogRequest;
import com.sakura.mall.ware.undoLog.domain.request.UndoLogPageRequest;
import com.sakura.mall.ware.undoLog.domain.vo.UndoLogVo;
import com.sakura.mall.ware.undoLog.mapper.UndoLogMapper;
import com.sakura.mall.ware.undoLog.service.UndoLogService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *  service impl
 *
 * @author sakura
 */
@Service
@RequiredArgsConstructor
public class UndoLogServiceImpl extends ServiceImpl<UndoLogMapper, UndoLogEntity> implements UndoLogService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddUndoLogRequest request) {
        // todo 其他校验逻辑
        UndoLogEntity undoLog = request.convert(UndoLogEntity.class);
        this.save(undoLog);
    }

    @Override
    public Page<UndoLogVo> queryPage(UndoLogPageRequest request) {
        IPage<UndoLogEntity> qp = request.toPage();
        LambdaQueryWrapper<UndoLogEntity> queryWrapper = new LambdaQueryWrapper<>();
        this.page(qp, queryWrapper);
        IPage<UndoLogVo> ret = qp.convert(entity -> entity.convert(UndoLogVo.class));
        return Page.convert(ret);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Long id, UpdateUndoLogRequest request) {
        UndoLogEntity entity = request.convert(UndoLogEntity.class);
        entity.setId(id);
        this.updateById(entity);
    }

}

